#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Attempt to backup from a fifo and restore to a fifo
#
TestName="fifo-test"
JobName=FIFOTest
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/tmp/bfifo" >${cwd}/tmp/file-list

rm -f ${cwd}/tmp/bfifo
mkfifo ${cwd}/tmp/bfifo
# send a file into the fifo
file=encrypt-bug.jpg
cat ${file} >${cwd}/tmp/bfifo&

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=TestVolume001 slot=1 drive=0
setdebug level=10 storage=File1
run job=$JobName storage=File1 yes
wait
messages
list volumes
sql
select jobid,mediaid,startblock,endblock from JobMedia;

quit
END_OF_DATA


run_bacula

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
@#setdebug level=50 client=$CLIENT
restore where=/  select all storage=File1 done
yes
wait
messages
quit
END_OF_DATA

cat <${cwd}/tmp/bfifo >${cwd}/tmp/${file}&

run_bconsole

check_for_zombie_jobs storage=File1
stop_bacula

if [ "$debug" = 1 ] ; then
  $bin/bls -d 50 -v -c $conf/bacula-sd.conf -V 'TestVolume001' FileStorage1
  ls -l ${cwd}/tmp/TestVolume*
fi

check_two_logs
diff ${file} ${cwd}/tmp/${file} >/dev/null 2>&1
dstat=$?
end_test
